package com.smartappsnetwork.personalexpenses;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.smartappsnetwork.personalexpenses.adapters.CategoryAdapter;
import com.smartappsnetwork.personalexpenses.adapters.CurrencyAdapter;
import com.smartappsnetwork.personalexpenses.adapters.PayModeAdapter;
import com.smartappsnetwork.personalexpenses.customobjects.CustomDatePickerDialog;
import com.smartappsnetwork.personalexpenses.dataaccess.CategoryDA;
import com.smartappsnetwork.personalexpenses.dataaccess.CurrencyDA;
import com.smartappsnetwork.personalexpenses.dataaccess.ExpencesDA;
import com.smartappsnetwork.personalexpenses.dataaccess.PayModeDA;
import com.smartappsnetwork.personalexpenses.database.DatabaseHelper;
import com.smartappsnetwork.personalexpenses.dataobject.CategoryDO;
import com.smartappsnetwork.personalexpenses.dataobject.CurrencyDO;
import com.smartappsnetwork.personalexpenses.dataobject.ExpenceDO;
import com.smartappsnetwork.personalexpenses.dataobject.PayModeDO;

/**
 * A fragment that launches other parts of the demo application.
 */
public class ExpencesFragment extends Fragment 
{
	Button btnDate;
	TextView tvDate;
	CustomDatePickerDialog datePickerDialog;
	int mColor = 2234354;
	int year, day, month;
	SimpleDateFormat outFormat;
	Date date;
	String dayinstr;
	
	View rootView;
	
	Spinner spnCategory, spnCurrency, spnPayMode;
	EditText edtAmount, edtDescription;
	Button btnAddExpenses;
	
	Vector<CategoryDO> vecCategoryDO;
	Vector<PayModeDO> vecPayModeDO;
	Vector<CurrencyDO> vecCurrencyDO;

	CategoryAdapter categoryAdapter;
	PayModeAdapter payModeAdapter;
	CurrencyAdapter currencyAdapter;
	
	CategoryDO categoryDO;
	CurrencyDO currencyDO;
	PayModeDO payModeDO;
	
	String dateselected;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        rootView = inflater.inflate(R.layout.expences_input_activity, container, false);
        initializeControls();
      
        tvDate.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				datePickerDialog = new CustomDatePickerDialog(getActivity(), new OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
					{
						 date = new Date(year, monthOfYear, dayOfMonth-1);
						    
						    dayinstr = outFormat.format(date);
						    tvDate.setText(dayinstr+", "+(dayOfMonth) +"-"+ (monthOfYear+1) +"-"+ year);
						    
						    dateselected = (dayOfMonth) +"-"+ (monthOfYear+1) +"-"+ year;
					}
				}, 2000, 0, 1, year, month, day);
				datePickerDialog.show();
			}
		});
        
        spnCategory.setOnItemSelectedListener(new OnItemSelectedListener() 
        {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
			{
				categoryDO = categoryAdapter.getItem(position);
				Log.e("category name", "category : "+categoryDO.name);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) 
			{
				
			}
		});
        
        spnCurrency.setOnItemSelectedListener(new OnItemSelectedListener() 
        {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
			{
				currencyDO = currencyAdapter.getItem(position);
				Log.e("currencyDO name", "currencyDO : "+currencyDO.name);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) 
			{
				
			}
		});

        spnPayMode.setOnItemSelectedListener(new OnItemSelectedListener() 
        {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
			{
				payModeDO = payModeAdapter.getItem(position);
				Log.e("payModeDO name", "payModeDO : "+payModeDO.name);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) 
			{
				
			}
		});
        
        
        btnAddExpenses.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				if(edtAmount.getText().toString().equalsIgnoreCase("") || edtDescription.getText().toString().equalsIgnoreCase(""))
				{
					Toast.makeText(getActivity(), "Please provide correct info.", Toast.LENGTH_SHORT).show();
				}
				else
				{
					long amount = Long.parseLong(edtAmount.getText().toString());
					String desc = edtDescription.getText().toString();
					
					ExpenceDO expenceDO = new ExpenceDO();
					expenceDO.amount = amount;
					expenceDO.description = desc;
					expenceDO.date = dateselected;
					expenceDO.cid = categoryDO.cid;
					expenceDO.pid = payModeDO.pid;
					expenceDO.curencyid = currencyDO.currencyid;
					
					long i = new ExpencesDA().insertExpences(expenceDO);
					if(i>=0)
					{
						edtAmount.setText("");
						edtDescription.setText("");
						Toast.makeText(getActivity(), "Data Inserted", Toast.LENGTH_SHORT).show();
					}
					else
					{
						Toast.makeText(getActivity(), "Failed to insert", Toast.LENGTH_SHORT).show();
					}
							
				}
			}
		});
        
        return rootView;
    }

    public void initializeControls()
    {
    	
    	  tvDate = (TextView) rootView.findViewById(R.id.tvDate);
          edtAmount = (EditText) rootView.findViewById(R.id.edtAmount);
          edtDescription = (EditText) rootView.findViewById(R.id.edtDescription);
          btnAddExpenses = (Button) rootView.findViewById(R.id.btnAddExpense);
    	  
          
          final Calendar c = Calendar.getInstance();
          year = c.get(Calendar.YEAR);
          month = c.get(Calendar.MONTH);
          day = c.get(Calendar.DAY_OF_MONTH);
          date = new Date(year, month, day);
          outFormat = new SimpleDateFormat("EEE");
          dayinstr = outFormat.format(date);
          tvDate.setText(dayinstr+", "+day +"-"+ (month+1) +"-"+ year);
          dateselected = day +"-"+ (month+1) +"-"+ year;
          
          spnCategory = (Spinner) rootView.findViewById(R.id.spnCategory);
          spnPayMode = (Spinner) rootView.findViewById(R.id.spnPayMode);
          spnCurrency = (Spinner) rootView.findViewById(R.id.spnCurrency);
          
          new DatabaseHelper(getActivity());
          
          vecCategoryDO =  new CategoryDA().getAllCategory();
          vecPayModeDO = new PayModeDA().getAllPayModes();
          categoryAdapter = new CategoryAdapter(getActivity(),vecCategoryDO);
          categoryAdapter.setHideIcon(false);
          categoryAdapter.setViewClicable(false);
          
          payModeAdapter = new PayModeAdapter(getActivity(), vecPayModeDO);
          payModeAdapter.setViewIcon(false);
          payModeAdapter.setViewClicable(false);
          
          spnPayMode.setAdapter(payModeAdapter);
          spnCategory.setAdapter(categoryAdapter);
          
          
          vecCurrencyDO = new CurrencyDA().getAllCurrency();
          currencyAdapter = new CurrencyAdapter(getActivity(), vecCurrencyDO);
          currencyAdapter.setViewClicable(false);
          currencyAdapter.setViewIcon(false);
          spnCurrency.setAdapter(currencyAdapter);
          
    	
    }
	
  
}
